package com.sgb.jicai.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.sgb.jicai.R;
import com.sgb.jicai.databinding.FragmentJicaiGuidanceBinding;
import com.sgb.jicai.entity.OfferAdviceListEntity;
import com.sgb.jicai.view.adapter.OfferAdviceListAdapter;
import com.sgb.jicai.viewmodel.JiCaiGuidanceFraModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.customview.ViewPagerForScrollView;
import java.util.List;

/**
 * 集采指导页面
 * 加盟集采、参与报价  页面的 报价指导和加盟指导  复用
 */
public class JiCaiGuidanceFragment extends AppLazyFragment {

    JiCaiGuidanceFraModel model;
    private FragmentJicaiGuidanceBinding binding;
    //单号
    private String jicaiNo;
    public static final String JICAI_NO = "JiCaiNo";

    private OfferAdviceListAdapter adapter;

    private int pagerIndex;
    private ViewPagerForScrollView pager;

    public JiCaiGuidanceFragment(int pagerIndex, ViewPagerForScrollView pager) {
        this.pagerIndex = pagerIndex;
        this.pager = pager;
    }

    public static JiCaiGuidanceFragment newInstance(String jicaiId, ViewPagerForScrollView pager, int pagerIndex) {
        JiCaiGuidanceFragment fragment = new JiCaiGuidanceFragment(pagerIndex, pager);
        Bundle bundle = new Bundle();
        bundle.putString(JICAI_NO, jicaiId);
        fragment.setArguments(bundle);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_jicai_guidance, container, false);
        model = new ViewModelProvider(this).get(JiCaiGuidanceFraModel.class);
        model.setActivity(JiCaiGuidanceFragment.this);
        if (getArguments() != null) {
            jicaiNo = getArguments().getString(JICAI_NO);
        }
        pager.setObjectForPosition(binding.getRoot(), pagerIndex);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.shape_list_divide_joinin_details_jc));
        if (null == adapter) {
            adapter = new OfferAdviceListAdapter(getContext(), null);
        }
        binding.recycler.addItemDecoration(divider);
        binding.recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recycler.setNestedScrollingEnabled(false);
        binding.recycler.setAdapter(adapter);
        binding.recycler.setLoadingMoreEnabled(false);
    }

    @Override
    protected void onLazyLoad() {
        model.getOfferAdviceData(jicaiNo);
    }

    public void updateOfferAdviceView(List<OfferAdviceListEntity> offerAdviceList) {
        if (offerAdviceList.size()>0){
            if (null != offerAdviceList && !offerAdviceList.isEmpty()) {
                adapter.setDatas(offerAdviceList);
            } else {
                adapter.setDatasMayNull(null);
            }
        }else {
            binding.recycler.setVisibility(View.GONE);
            binding.rlNullView.setVisibility(View.VISIBLE);
        }

    }

}
